home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / DESKLIBC / SOURCES.ZIP / DeskLib / !DLSources / Libraries / Mem / c / CheckHeap next >
Text File  |  1995-07-08  |  4KB  |  155 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Mem.CheckHeap.c
  12.     Author:  Copyright © 1994, 1995 Jason Howat
  13.     Version: 2.00 (18 Jun 1995)
  14.     Purpose: Dynamic memory manager - checks validity of heap
  15. */
  16.  
  17. #include "MemDefs.h"
  18.  
  19. #include "DeskLib:Error.h"
  20.  
  21. #ifdef MEM__DEBUG
  22. #include <stdio.h>
  23. #endif
  24.  
  25.  
  26. extern BOOL Mem_CheckHeap(void)
  27. /*  Returns TRUE if the heap data structure is valid (i.e. the links are all
  28.  *  intact and anchors are consistent)
  29.  */
  30. {
  31.   mem_header *chunk = (mem_header *) mem__heap,
  32.              *next = chunk,
  33.              *end_of_heap = (mem_header *) (mem__heap + mem__heapsize);
  34.   int         free = 0;
  35.  
  36.   while(TRUE)
  37.   {
  38. #ifdef MEM__DEBUG
  39. fprintf(stderr, "[%p,%p,%8x,%8x,%8x]\n",
  40.                 chunk,
  41.                 chunk->handle,
  42.                 chunk->prevrealsize,
  43.                 chunk->realsize,
  44.                 chunk->datasize);
  45. fflush(stderr);
  46. #endif
  47.  
  48.     if(ISFREE(chunk))
  49.       free += chunk->realsize;
  50.     else
  51.       free += chunk->realsize - CHUNKSIZE(chunk->datasize);
  52.  
  53.     if(!ISFREE(chunk) &&
  54.        ((int) *(chunk->handle) != ((int) chunk + sizeof(mem_header))))
  55.     {
  56.       Error_Report(0, "Mem heap corrupt: Inconsistent chunk handle and anchor (chunk %p)",
  57.                       chunk);
  58. #ifdef MEM__DEBUG
  59. fprintf(stderr, "Mem heap corrupt: Inconsistent chunk handle and anchor (chunk %p)\n", chunk);
  60. fflush(stderr);
  61. #endif
  62.       return(FALSE);
  63.     }
  64.  
  65.     if(chunk->realsize < chunk->datasize + sizeof(mem_header))
  66.     {
  67.       Error_Report(0, "Mem heap corrupt: Data larger than chunk (chunk %p)",
  68.                       chunk);
  69. #ifdef MEM__DEBUG
  70. fprintf(stderr, "Mem heap corrupt: Data larger than chunk (chunk %p)\n", chunk);
  71. fflush(stderr);
  72. #endif
  73.       return(FALSE);
  74.     }
  75.  
  76.     next = (mem_header *) ((int)chunk + chunk->realsize);
  77.     if(next >= end_of_heap)
  78.       break;
  79.  
  80.     if(chunk >= next)
  81.     {
  82.       Error_Report(0, "Mem heap corrupt: Backward link (chunk %p)",
  83.                       chunk);
  84. #ifdef MEM__DEBUG
  85. fprintf(stderr, "Mem heap corrupt: Backward link (chunk %p)\n", chunk);
  86. fflush(stderr);
  87. #endif
  88.       return(FALSE);
  89.     }
  90.  
  91.     if(chunk->realsize != next->prevrealsize)
  92.     {
  93.       Error_Report(0, "Mem heap corrupt: Chunk link(s) corrupt (chunks %p and %p)",
  94.                       chunk, next);
  95. #ifdef MEM__DEBUG
  96. fprintf(stderr, "Mem heap corrupt: Chunk link(s) corrupt (chunks %p and %p)\n", chunk, next);
  97. fflush(stderr);
  98. #endif
  99.       return(FALSE);
  100.     }
  101.  
  102.     if(ISFREE(chunk) && ISFREE(next))
  103.     {
  104.       Error_Report(0, "Mem heap corrupt: Two consecutive free chunks (chunks %p and %p)",
  105.                       chunk, next);
  106. #ifdef MEM__DEBUG
  107. fprintf(stderr, "Mem heap corrupt: Two consecutive free chunks (chunks %p and %p)\n", chunk, next);
  108. fflush(stderr);
  109. #endif
  110.       return(FALSE);
  111.     }
  112.  
  113.     chunk = next;
  114.   }
  115.  
  116.   if(next > end_of_heap)
  117.   {
  118.     Error_Report(0, "Mem heap corrupt: Last chunk too big");
  119. #ifdef MEM__DEBUG
  120. fprintf(stderr, "Mem heap corrupt: Last chunk too big\n");
  121. fflush(stderr);
  122. #endif
  123.     return(FALSE);
  124.   }
  125.  
  126.   if(chunk != mem__lastchunk)
  127.   {
  128.     Error_Report(0, "Mem heap corrupt: mem__lastchunk not set correctly (is %p, should be %p)",
  129.                     mem__lastchunk, chunk);
  130. #ifdef MEM__DEBUG
  131. fprintf(stderr, "Mem heap corrupt: mem__lastchunk not set correctly (is %p, should be %p)\n", mem__lastchunk, chunk);
  132. fflush(stderr);
  133. #endif
  134.     return(FALSE);
  135.   }
  136.  
  137. #ifdef MEM__DEBUG
  138. fprintf(stderr, "mem__free: %8x\n---\n",mem__free);
  139. fflush(stderr);
  140. #endif
  141.  
  142.   if(free != mem__free)
  143.   {
  144.     Error_Report(0, "Mem heap corrupt: mem__free not consistent with actual free space (is %x, should be %x)",
  145.                     mem__free, free);
  146. #ifdef MEM__DEBUG
  147. fprintf(stderr, "Mem heap corrupt: mem__free not consistent with actual free space (is %x, should be %x)\n", mem__free, free);
  148. fflush(stderr);
  149. #endif
  150.     return(FALSE);
  151.   }
  152.  
  153.   return(TRUE);
  154. }
  155.